/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.util.Date;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mrp.productionPlanning.entity.plannedDemandEntity;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class plannedDemandSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    
    private plannedDemandEntity pde;
    
    public void addPlannedDemand(plannedDemandEntity p){
        em.persist(p);
    }
    
    public void updatePlannedDemand(plannedDemandEntity p){
        em.merge(p);
    }
    
    public void removePlannedDemand(plannedDemandEntity p){
        em.remove(p);
    }
    
    public plannedDemandEntity getPlannedDemandEntityByDate (Date d){
        Query query = em.createQuery("SELECT u FROM PlannedDemandEntity u where u.date = :dd");
        query.setParameter("dd", d);
        plannedDemandEntity pde = null;
        try {
            pde = (plannedDemandEntity) query.getSingleResult();
        } catch (NoResultException ex) {
            throw ex;
        }
        return pde;
    }
    
    public plannedDemandEntity getPlannedDemandEntityById (Long id){
        return em.find(plannedDemandEntity.class, id);
    }
    
    
    
}
